package com.itheima.sercice.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.sercice.OrderSettingService;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 文件上传
     *
     * @param list
     * @return
     */
    @Override
    public void add(List<OrderSetting> list) {
        //批量导入预约人数
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行了预约设置
                Integer countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderDate > 0) {
                    //已经进行了预约设置,执行更新操作
                    orderSettingDao.updateByOrderDate(orderSetting);
                } else {
                    //没有进行预约设置,执行插入操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * 根据日期修改可预约人数
     *
     * @param orderSetting
     * @return
     */
    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        //查询当前日期是否进行了预约设置
        Integer count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingDao.updateByOrderDate(orderSetting);
        } else {
            //当前日期未进行预约设置,需要进行插入操作
            orderSettingDao.add(orderSetting);
        }
    }

    /**
     * 根据年月查询对应的预约设置信息
     *
     * @param date
     * @return
     */
    @Override
    public List<OrderSetting> getOrderSettingByMonth(String date) throws Exception {
        String begin = date + "-1";
        String end = date + "-31";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date beginTime = sdf.parse(begin);
        Date endTime = sdf.parse(end);
        Map<String, Date> map = new HashMap<>();
        map.put("begin", beginTime);
        map.put("end", endTime);

        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);

        return list;


    }

    /**
     * 模板下载
     *
     * @param response
     * @return
     */
    @Override
    public void download(HttpServletResponse response, String filename) {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("templates/" + filename);
        if (in == null) {
            // 处理文件未找到的情况
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        try {
            XSSFWorkbook excel = new XSSFWorkbook(in);

            // 设置响应的内容类型
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            // 设置响应的头部，指示这是一个要下载的附件，并指定下载后的文件名
            String headerKey = "Content-Disposition";
            String headerValue = String.format("attachment; filename=\"%s\"", URLEncoder.encode(filename, "UTF-8"));
            response.setHeader(headerKey, headerValue);

            ServletOutputStream out = response.getOutputStream();
            excel.write(out);


            out.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
